package com.baselibrary.base.mvvm

import android.Manifest
import android.app.Activity
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.baselibrary.UserInfo
import com.baselibrary.bean.*
import com.baselibrary.net.APIResponse
import com.baselibrary.permission.PermissionCollection
import com.baselibrary.permission.PermissionsUtils
import com.baselibrary.permission.callback.PermissionsCall
import com.baselibrary.utils.AppManager
import com.baselibrary.utils.PopUtils
import com.baselibrary.utils.SpUtils

/**
 * @description
 * @author 关忆北、
 * @time 2022/3/30 10:09 上午
 */
public open class BaseViewModel : ViewModel() {
    var followLiveData: MutableLiveData<FollowSuccBean> = MutableLiveData()//关注
    var subscribeLiveData: MutableLiveData<Boolean> = MutableLiveData()//专栏订阅
    var collectionLiveData: MutableLiveData<Boolean> = MutableLiveData()//收藏
    var supportLiveData: MutableLiveData<Boolean> = MutableLiveData()//点赞
    var friendLiveData: MutableLiveData<List<FriendListBean>> = MutableLiveData()//好友列表
    var homeStreamLD: MutableLiveData<StreamData.DataDTO> = MutableLiveData()
    var dynamicStreamLD: MutableLiveData<DynamicInfoBean.DataBeanX> = MutableLiveData()
    var deleteTimelineLD: MutableLiveData<String> = MutableLiveData()//删除动态
    var giveCoinLD: MutableLiveData<Int> = MutableLiveData()//各种操作成功获取酒币
    var shareAddNumLD: MutableLiveData<Any> = MutableLiveData()//分享+1
    var seckillAddSubLD: MutableLiveData<SeckillAddSubscribeBean> = MutableLiveData()//秒杀-添加预约
    var seckillCancelSubLD: MutableLiveData<Any> = MutableLiveData()//秒杀-取消预约
    var reqComplateLD: MutableLiveData<Boolean> = MutableLiveData(false)//酒窖详情的 --评论、下酒菜、动态、接口请求完成后
    var bannerConfigListLD: MutableLiveData<BannerConfigListBean> = MutableLiveData()//banner 列表
    var hotWordListLD: MutableLiveData<List<HotWordListBean>> = MutableLiveData()//热词

    var reqComplateCnt = 0
    fun requestComplate() {
        reqComplateCnt++
        if (reqComplateCnt >= 4) {
            reqComplateLD.value = true
            reqComplateCnt = 0
        } else {
            reqComplateLD.value = false
        }
    }

    //首页瀑布流
    fun requestStream(type: Int, page: String, cursor: String) {
        BaseRequestManager.getInstance().requestStream(type, homeStreamLD, page, cursor)
    }

    //频道瀑布流
    fun requestChannel(channelId: Int, page: String, cursor: String) {
        BaseRequestManager.getInstance().requestChannel(dynamicStreamLD, channelId, page, cursor)
    }

    //最热或者最新
    fun requestHotAndLatest(page: String, tid: String, type: String) {
        BaseRequestManager.getInstance().requestHotAndLatest(dynamicStreamLD, page, tid, type)
    }

    //用户收藏的动态和话题
    fun requestUserCollection(page: String, type: String, userId: String) {
        BaseRequestManager.getInstance().requestUserCollection(dynamicStreamLD, page, type, userId)
    }

    //用户赞过的动态
    fun requestUserLiked(page: String, userId: String) {
        BaseRequestManager.getInstance().requestUserLiked(dynamicStreamLD, page, userId)
    }

    //用户发布的动态
    fun requestUserTimeline(page: String, uid: String) {
        BaseRequestManager.getInstance().requestUserTimeline(dynamicStreamLD, page, uid)
    }

    //酒窖发布的动态
    @Deprecated("目前都使用用户动态")
    fun requestWineryTimeline(page: String, wineryCode: String) {
        BaseRequestManager.getInstance().requestWineryTimeline(dynamicStreamLD, page, wineryCode)
    }

    //话题内搜索
    fun requestSearchTopic(page: String, tid: String, keyword: String) {
        BaseRequestManager.getInstance().requestSearchTopic(dynamicStreamLD, page, tid, keyword)
    }

    fun reqAddShareNum(id: Long) {
        BaseRequestManager.getInstance().reqAddShareNum(id, shareAddNumLD)
    }

    /**
     * @param  isFollow true = 进行关注
     */
    fun reqFollow(activity: Activity, isFollow: Boolean, type: Int, thirdId: Int) {
        if (!SpUtils.getInstance().isLogin) {
            AppManager.getAppManager().openLoginPage()
            return
        }
        if (isFollow) {
            BaseRequestManager.getInstance().reqAddFollow(followLiveData, type, thirdId)
        } else {
            PopUtils.showCancelFollow(activity, object : PopUtils.ICallBack {
                override fun clickOk() {
                    BaseRequestManager.getInstance().reqCancelFollow(followLiveData, type, thirdId)
                }

                override fun clickCancel() {

                }

            })
        }

    }

    /**
     * @param  isCollection true = 收藏
     * type"类型：1文章 2产品 3课程 4动态 5讲坛 6专栏 7约局 8新版下酒菜
     */
    fun reqCollection(isCollection: Boolean, type: Int, typeId: Long) {
        if (!SpUtils.getInstance().isLogin) {
            AppManager.getAppManager().openLoginPage()
            return
        }
        if (isCollection) {
            BaseRequestManager.getInstance().reqAddCollection(collectionLiveData, type, typeId)
        } else {
            BaseRequestManager.getInstance().reqCancelCollection(collectionLiveData, type, typeId)
        }

    }

    /**点赞类型 1评论的点赞 2文章点赞 3点评点赞 4动态  6课程 7直播间 9新版下酒菜
     * @param  isSupport true = 点赞
     */
    fun reqSupport(isSupport: Boolean, type: Int, typeId: Int) {
        if (!SpUtils.getInstance().isLogin) {
            AppManager.getAppManager().openLoginPage()
            return
        }
        if (isSupport) {
            BaseRequestManager.getInstance().reqAddSupport(supportLiveData, type, typeId)
        } else {
            BaseRequestManager.getInstance().reqCancelSupport(supportLiveData, type, typeId)
        }

    }

    /**
     * 好友列表
     * 分享弹窗-- 好友
     */
    fun reqFriendList(keyWord: String = "") {
        BaseRequestManager.getInstance().reqFriendList(keyWord, friendLiveData)
    }

    /**
     * 删除动态
     */
    fun deleteTimeline(tid: String) {
        BaseRequestManager.getInstance().deleteTimeline(deleteTimelineLD, tid)
    }

    /**
     * 奖励酒币
     * 1,"评论获取","
    2,"分享获取","
    3,"签到获取","
    4,"发布动态获取"
    5,"下单获取","
    6,"报名约局","
    7,"创建约局","
    8,"扫商品溯源码"
    9,"评价商品","
    11,"健康数据同步
     */
    fun reqGiveCoin(type: Int) {
        BaseRequestManager.getInstance().reqGiveCoin(giveCoinLD, type)
    }


    fun changeFriend(list: List<FriendListBean>): MutableList<ShareItemBean> {
        val shareList = mutableListOf<ShareItemBean>()
        list.forEach {
            shareList.add(ShareItemBean(it.name, it.icon, 0, it.friendId))
        }
        return shareList
    }

    fun loadPermissions(activity: Activity, listener: PermissionsCall) {
        PermissionsUtils.getInstance().chekPermissions(activity, PermissionCollection.CUSTOM.addPermissionList(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA), listener)
    }

    /**
     * 秒杀--添加预约
     */
    fun reqSeckillAddSubscribe(actId: String) {
        BaseRequestManager.getInstance().reqSeckillAddSubscribe(seckillAddSubLD, actId)
    }

    /**
     * 秒杀--取消预约
     */
    fun reqSeckillCancelSubscribe(actId: String) {
        BaseRequestManager.getInstance().reqSeckillCancelSubscribe(seckillCancelSubLD, actId)
    }

    /**根据类型获取banner列表（首页大改版）
     * @time: 2023/4/20 11:48
     * @description:    bannerType： banner类型 0首页banner 1商城banner 2附近banner 3中心banner 4话题轮播图
     */
    fun reqGetBannerConfigList(bannerType: Int,otherId:Long=0) {
        BaseRequestManager.getInstance().reqGetBannerConfigList(bannerType,otherId,bannerConfigListLD)
    }

    /**根据类型获取热词（首页大改版）
     * @time: 2023/4/20 11:48
     * @description:     areaType： 位置0首页 1商城
     */
    fun reqGetHotWordList(areaType: Int) {
        BaseRequestManager.getInstance().reqGetCarouselHotWordList(areaType,hotWordListLD)
    }

    /**
     * 更新用户信息
     */
    fun reqUpUserInfo() {
        BaseRequestManager.getInstance().reqUpUserInfo(object : APIResponse<UserInfo.DataBean>(this) {
            override fun onSuccess(response: UserInfo.DataBean, json: String) {
                var u= UserInfo();
                response.token=SpUtils.getInstance().getUserToken()
                u.data=response
                SpUtils.getInstance().putLoadUserBean(u);
            }

            override fun onError(e: Throwable) {
                super.onError(e)
            }
        })
    }

}